/*
 * @Author: DUST
 * @Date: 2023-06-28 09:18:28
 * @LastEditors: DUST
 * @LastEditTime: 2023-07-03 16:56:27
 * @FilePath: \koa-node-ts\app\service\WebsiteService.ts
 * @Description: 
 * undefined
 */
import { Sequelize } from "sequelize-typescript";
import Website from "../model/Website";
import { WebsiteItem } from "../validator/WebsiteValidator";

class WebsiteService {
  addWebsite(values: any){
    return Website.findOrCreate({
      where: { url: values.url },
      defaults: values
    })
  }
  getWebsiteList(pageNumber: number, pageSize: number) {
    return Website.findAndCountAll({
      limit: pageSize,
      offset: (pageNumber - 1) * pageSize,
      attributes: [
        "name",
        "url",
        "logoUrl",
        "createTime",
        "upTime",
        "description",
        "id",
        "type",
        [
          Sequelize.literal(
            "(select name from Navigate where Navigate.id = Website.type)"
          ),
          "typeName",
        ],
      ],
      order: [['createTime', "desc"], ["upTime", "desc"]]
    })
  }
  editWebsite(values: WebsiteItem, id: number) {
    return Website.update(values, {
      where: { id }
    })
  }
  deleteWebsite(id: number) {
    return Website.destroy({
       where: { id }
    })
  }

  getListByType(type: number) {
    return Website.findAll({
      where: { type }
    })
  }
}

export default new WebsiteService;